module Explode.ReplayGameState;
private import Explode.Editor;
private import Explode.GameState;
private import Explode.GameTitleState;
private import Explode.GameoverState;
private import Explode.PauseMenuState;
private import Explode.Stage;
private import Explode.Player;
private import Explode.GameConfig;
private import Explode.ScoreBoard;
private import Explode.StageSelector;
private import Form.MenuItem;
private import State.IState;
private import Input.KeyCode;
private import Input.GamePad;
private import Input.Mouse;
private import Input.InputRecorder;
private import Graphics.IGraphics;
private import Sound.Manager;
private import System.Console;
private import std.file;
private import std.string;
private import std.format;
private import std.random;


public class ReplaySelectState : BasicState {
	public const int SELECT      = 1;
	public const int CANCEL      = 0;
	protected int _returnValue = 0;
	public int returnValue(){return _returnValue;}

	protected const int WIDTH = 320 , HEIGHT = 400;
	protected string _title = "REPLAY";
	protected int _bestTime = -1;
	private class Background : IDrawingEntry{
		public float x(){return 0;}
		public float y(){return 0;}
		public float z(){return 200;}
		public bool drawingEntry( IGraphics g ){
			return g.addDrawingEntry( this );
			
		} //
		public bool draw( IGraphics g ){
			g.setViewport( 0 , 0, WIDTH , HEIGHT );
			g.drawFilledRect( 0 , 0 , WIDTH , HEIGHT , Color.create(0x33000000u) );
			string txt  = _title;
			string btxt = format("BEST TIME %06d" , _bestTime );
			int ww = 32 , hh = 64;
			_pstage.draw( g );
				g.drawText( txt , ( WIDTH - ww * txt.length  ) / 2 , 70 , Color.create( 0x66ffffffu ) , ww , hh );
				g.drawText( txt , ( WIDTH - ww * txt.length  ) / 2 , 50 , Color.create( 0xccff0000u ) , ww , hh );
			ww = 8; hh = 16;
			if ( _bestTime > 0 )
				g.drawText( btxt 
						, ( WIDTH - ww * btxt.length ) / 2 
						, (HEIGHT - hh) / 2 
						, Color.create( 0xff888800u ) , ww , hh );

			g.drawFilledRect( 64 , HEIGHT / 2 + 85 , WIDTH - 128 ,  25 * 4 , Color.create( 0x330000ffu ) , 0 );
			return true;
		} //
	} //

	private IState _caller;

	private int _frameCnt;
	protected IGamePad _gamepad;
	protected IMenuItem[] _items; 
	protected MenuItem _cancelMenu;
	protected EnumMenuItem _stageMenu , _autoMenu , _shuffleMenu;
	
	protected IMenuItem _activeItem ;
	protected ReplayGameState _replayState;
	protected IState _master,_nextState ;
	protected IGraphics _graphics;

	protected string _fileName;
	protected string[] _stageValues;
	private  Background _background;
	protected PreviewStage _pstage;
	
	protected string _baseDir ;
	
	public string title() { return _title; } //
	public void title( string v ) { _title = v; } //

	public this( IState e , IGraphics g ){
		_master    = e;
		_graphics  = g;
	} //


	public IState call( IState caller ) {
		_caller = caller;
		_nextState = this;
		initMenu();
		_gamepad.reset();
		_returnValue = 0;
		_frameCnt = 0;
		if ( _stageValues.length == 0 ) return _master.call( this );
		SoundManager.background.stop();
		if ( caller is _master ) {
			setSelectValues( 0 );
		}
		return this;
	} //

	protected void initMenu() {
		if ( _cancelMenu is null ) {
			Console.write("Create ReplaySelectMenu...");
			_replayState = new ReplayGameState( this , _graphics );
			_background  = new Background();
			_gamepad     = new VirtualGamePad( 8 );
			_pstage		= new PreviewStage( _graphics );
			Console.write("1");

			_stageMenu   = new EnumMenuItem("FILE SELECT    ");
			_autoMenu    = new EnumMenuItem("AUTO PLAY      ");
			_shuffleMenu = new EnumMenuItem("SHUFFLE        ");
			_cancelMenu  = new MenuItem("CANCEL");
			
			_items ~= _stageMenu;
			_items ~= _autoMenu;
			_items ~= _shuffleMenu;
			_items ~= _cancelMenu;
			Console.write("2");

			MenuItemConnector.connectVertical( _items );
			_activeItem = _stageMenu.activate();
			
			int xx = WIDTH / 2 , yy = HEIGHT / 2 + 60;
			_stageMenu.moveTo(   xx , yy +=  25 );
			_autoMenu.moveTo(    xx , yy +=  25 );
			_shuffleMenu.moveTo( xx , yy +=  25 );
			_cancelMenu.moveTo(  xx , yy +=  25 );

			Console.write("3");

			_autoMenu.setValues(    "AUTO PLAY" , ["NO","YES"] , "NO" );
			_shuffleMenu.setValues( "SHUFFLE  " , ["NO","YES"] , "YES" );

			_stageMenu.setSelectHandler(  &onStageMenuSelect );
			_autoMenu.setSelectHandler(  &onStageMenuSelect );
			_shuffleMenu.setSelectHandler(  &onStageMenuSelect );
			_stageMenu.setChangedHandler(  &onStageMenuChange );
			_cancelMenu.setSelectHandler( &onCancelMenuSelect );
			setStageFile( setSelectValues( 0 ) );
			Console.writeln("OK");
		} else {
			reloadStage();
			Console.writeln("_stageMenu.value=%s" , _stageMenu.value );
		}
		while( !(_activeItem is _stageMenu) ) {
			_activeItem = _activeItem.down();
		}
	} //

	protected string setSelectValues( int index = 0 ) {
		string srcDir = GameConfig.stageDir ~"/replay/";
		string[] files = listdir(srcDir);
		string v = _stageMenu.value;
		if ( files.length < 1 ) return "";
		foreach ( int i , string s ; files ) {
			if ( find( s , ".txt" ) > 0 ) _stageValues ~= split( s , "." )[0];
		}
		if ( _stageValues.length < 1 ) return "";
		if ( v == "" ) v = _stageValues[ index ];
		_stageMenu.setValues( "FILE   " , _stageValues , v );
		return srcDir ~ _stageValues[0] ~ ".txt";
	} //

	protected int getBestTime( string file ) {
		if ( exists( file ) ) {
			string content = cast(string)read( file );
			string chk = split( content , "[body]" )[0];
			if ( chk.length > 1 ) {
				return cast(int)atoi( split( chk , "," )[0] );
			}
		}
		return -1;
	} //

	string _lastFile = "";
	public string setStageFile( string src ) {
		if ( !exists( src ) ) {
			Console.writeln("file not found %s" , src);
			if( !exists( src ) ) {
				return "";
			}
		}
		if ( _pstage.loadStageFromFile( src ) ) {
			_fileName = src;
			_bestTime = getBestTime( src );
			_lastFile= src;
			for ( int i = 0 ; i < 30 ; i++ ) _pstage.zoomOut();
		}
		return src;
	} //
	public void reloadStage() {
		setStageFile( _lastFile );
	} //

	int _cntY = 0, _cntX = 0;
	public IState action() {
		_frameCnt ++;
		foreach ( int i , IMenuItem item ; _items  ) {
			item.action();
		} //
		if ( _frameCnt > 30 ) {
			if ( _gamepad.y != 0 )  _cntY++; 
			else					_cntY = 0;
			if ( _gamepad.x != 0 )  _cntX++;
			else					_cntX = 0;
			if ( _cntY % 10 == 1 ) {
				_activeItem = ( _gamepad.y > 0 ) ? _activeItem.down  : _activeItem.up;
			}
			if ( _cntX % 10 == 1  || ( _cntX > 60 && _cntX % 3 == 1 ) ) {
				_activeItem = ( _gamepad.x > 0 ) ? _activeItem.right : _activeItem.left;
			}
		}
		if ( _activeItem is _stageMenu ) {
				 _pstage.action();
			if ( _pstage.frameCnt < 10 ) _pstage.zoomOut();
		}

		if ( _gamepad.getButton( 0 ) != 0 ) {
			_activeItem.select();
		}

		return _nextState;
	} //

	public IState draw( IGraphics g ) {
		foreach ( int i , IMenuItem item ; _items  ) {
			item.drawingEntry( g );
		} //
		_background.drawingEntry( g );
		g.draw();
		return this;
	} //


	public bool onKeyDown( int keyCode ){
		switch ( keyCode ) {
			case KeyCode.UP:
				_gamepad.y = -1;
				break;
			case KeyCode.DOWN:
				_gamepad.y =  1;
				break;
			case KeyCode.LEFT:
				_gamepad.x = -1;
				break;
			case KeyCode.RIGHT:
				_gamepad.x =  1;
				break;
			case KeyCode.Z:
			case KeyCode.SPACE:
			case KeyCode.T_ENTER:
			case KeyCode.ENTER:
				_gamepad.setButton( 0 , 1 );
				break;
			case KeyCode.X:
				_gamepad.setButton( 1 , 1 );
				break;
			default:
				break;
		}
		return true;
	} //
	public bool onKeyUp( int keyCode ){
		switch ( keyCode ) {
			case KeyCode.UP:
				_gamepad.y = 0;
				break;
			case KeyCode.DOWN:
				_gamepad.y = 0;
				break;
			case KeyCode.LEFT:
				_gamepad.x = 0;
				break;
			case KeyCode.RIGHT:
				_gamepad.x = 0;
				break;
			case KeyCode.Z:
			case KeyCode.ENTER:
				_gamepad.setButton( 0 , 0 );
				break;
			case KeyCode.X:
				_gamepad.setButton( 1 , 0 );
				break;
			default:
				break;
		}
		return true;
	} //
	public bool onJoyAxis( int deviceId , int axis , int delta ){
		if ( axis == JOYAXIS_X ) {
			if( delta != 0 ) _gamepad.x = ( delta > 0 ) ? 1 : -1;
			else			 _gamepad.x = 0;
		}
		if ( axis == JOYAXIS_Y ){
			if( delta != 0 ) _gamepad.y = ( delta > 0 ) ? 1 : -1;
			else			 _gamepad.y = 0;
		}
		return true;
	} //
	
	public bool onJoyButtonDown( int id , int button ) {
		int bm = GameConfig.getButtonMap( button );
		if ( bm == 9 ) { 
			_returnValue = CANCEL;
			_nextState = _caller.call( this );
		}
		else
		_gamepad.setButton( GameConfig.getButtonMap( button ) , 1 );
		return true;
	} //

	public bool onJoyButtonUp( int id , int button ) {
		_gamepad.setButton( GameConfig.getButtonMap( button ) , 0 );
		return true;
	} //



	public void onCancelMenuSelect( IMenuItem im ) {
		_nextState = _master.call( this );
		_fileName = "";
	} //

	public void onStageMenuSelect( IMenuItem im ) {
		string srcDir = GameConfig.stageDir ~"/replay/";
		_replayState.autoDemo = false;
		_nextState = _replayState.call( this );
		_replayState.setStageFile( _fileName );
		if ( _autoMenu.value	== "YES" )	_replayState.autoDemo = true;
		else								_replayState.autoDemo = false;
		if ( _shuffleMenu.value == "YES" )  _replayState.shuffle  = true;
		else								_replayState.shuffle  = false;
	} //

	
	public void onStageMenuChange( EnumMenuItem im , string selectedValue , int selectedIndex ) {
		setStageFile( GameConfig.stageDir ~ "/replay/" ~ selectedValue ~ ".txt" );
	} //

} //


/**
*	リプレイ画面用
**/
public class ReplayGameState : GameState{
	public static const int CLEAR = 100;
	public static const int DEAD  = 200;
	protected int _returnValue = 0;
	public int returnValue(){return _returnValue;}

	protected IState _caller;
	protected IState _master;
	protected bool _returnFlag;
	protected PauseScrollState _scroller; 
	protected string _fileName;
	protected int _bestTime;
	protected bool _tasFlag;
	
	protected bool _autoDemo = false;
	public bool autoDemo() { return _autoDemo; } //
	public void autoDemo( bool v ) { 
		_autoDemo = v; 
	} //

	protected bool _shuffle = false;
	protected bool _turbo = false;
	public bool shuffle() { return _shuffle; } //
	public void shuffle( bool v ) { _shuffle = v; } //

	public this( IState e ,IGraphics g ) {
		super( g );
		_master = e;
	} //

	public IState action() {
		_frameCnt++;

		if ( _zoom > 0 ) _stage.zoomIn();
		else if ( _zoom < 0 ) _stage.zoomOut();

		int cnt = 1;
		if ( _turbo ) cnt = 4;// 倍速再生
		for ( int i = 0 ; i < cnt ; i++ ) {
			_stage.action();
			if ( (cast(ReplayGameStage)_stage).replayEnd ) _pauseFlag = true;
			if ( _stage.mode == GameStage.MODE_DEAD ) {
				SoundManager.getSprite().play( _stage.missSound ).release();
				if ( _tasFlag ) {
					(cast(ReplayGameStage)_stage).reloadStage();
					_pauseFlag = false;
				} else {
					return _dying.call( _autoDemo ? this : _master );
				}
			} else 
			if ( _stage.mode == GameStage.MODE_CLEAR ) {
				SoundManager.getSprite().play( _stage.goalSound ).release();
				saveReplay();
				(cast(StageClearState)_stageClear).autoDemo = _autoDemo;

				return _stageClear.call( _autoDemo ? this : _master  );
			}
			if ( _returnFlag ) return _master.call( this );
			if ( _pauseFlag ) return _scroller.call( this );
		}
		return this;
	} //

	public IState call( IState caller ){
		_caller = caller;
		initObject();
		
		_zoom   = 0;
		_key_dn = 0;
		_key_up = 0;
		_key_lt = 0;
		_key_rt = 0;
		_param.reset();
		_pauseFlag = false;
		_returnFlag = false;
		_frameCnt = 0;
		if ( !(caller is _stageClear) )_turbo = false;

		if ( caller is _master ) {
			SoundManager.background.play( _stage.gameBgm , -1 );
			_gamepad.reset();
		} else 
		if ( caller is _scroller ) {
			if ( _scroller.returnValue == ReplayPauseScrollState.PLAY ) {
				(cast(ReplayGameStage)_stage).reloadStage();
				SoundManager.background.stop( );
				SoundManager.background.play( _stage.gameBgm , -1 );
				_gamepad.reset();
				_tasFlag = true;
			} else
			if ( _scroller.returnValue == ReplayPauseScrollState.TAS ) {
				(cast(ReplayGameStage)_stage).setTas();
				_tasFlag = true;
				return this;
			} else
				_gamepad.reset();
		} else
			_gamepad.reset();

		if ( _autoDemo && ( caller is _stageClear || caller is _dying ) ){
			bool b = _shuffle ? loadRandom() : loadNext();
			_tasFlag = false;
			_gamepad.reset();
			if ( ! b ) return _master.call( this );
		}
		return nextCall( caller );
	} //
	
	protected void initObject(){
		if ( _stage is null ) {
			_gamepad	   = new VirtualGamePad( 8 );
			_mouse		   = new BasicMouse();
			_param		   = new GameParam();
			_stage		   = new ReplayGameStage( _graphics , _param );
			_stage.gamepad = _gamepad;
			_stage.mouse   = _mouse;
			_scroller	   = new ReplayPauseScrollState( this , _param , _gamepad , _graphics );
			_stageClear	   = new StageClearState( _stage , _param , _gamepad );
			_dying		   = new DyingState( _stage , _param );
			setStageNumber( GameConfig.lastStage );
		}
	} //
/*
	public bool loadRandom() {
		string srcDir = GameConfig.stageDir ~"/replay/";
		string[] files = listdir(srcDir);
		if ( files.length < 0 ) return false;
		uint r = rand() % files.length;
		_fileName = srcDir ~ files[r];
		return _stage.loadStageFromFile( _fileName );
	} //
*/
	public bool loadRandom() {
		string srcDir = GameConfig.stageDir ~"/replay/";
		string[] filestmp = listdir(srcDir);
		if ( filestmp.length < 0 ) return false;

		string[] files;
		foreach ( int i , string file ; filestmp ) {
			if ( find( file , ".txt" ) >= 0 ) {
				files ~= file;
			}
		}

		uint r = rand() % files.length;
		_fileName = srcDir ~ files[r];
		return _stage.loadStageFromFile( _fileName );
	} //

	public bool loadNext() {
		string srcDir = GameConfig.stageDir ~"/replay/";

		string[] filestmp = listdir(srcDir);
		if ( filestmp.length < 0 ) return false;

		string[] files;
		foreach ( int i , string file ; filestmp ) {
			if ( find( file , ".txt" ) >= 0 ) {
				files ~= file;
			}
		}

		string nxt = _fileName;
		foreach ( int i , string f ; files ) {
			string gs = srcDir ~ f;
			if ( gs == _fileName ) {
				nxt = srcDir ~ files[ (i+1) % files.length ];
			}
		}
		_fileName = nxt;
		return _stage.loadStageFromFile( _fileName );
	} //


	protected int getBestTime( string file ) {
		if ( exists( file ) ) {
			string content = cast(string)read( file );
			string chk = split( content , "[body]" )[0];
			if ( chk.length > 1 ) {
				return cast(int)atoi( split( chk , "," )[0] );
			}
		}
		return -1;
	} //

	string _lastFile = "";
	public string setStageFile( string src ) {
		if ( !exists( src ) ) {
			Console.writeln("file not found %s" , src);
			if( !exists( src ) ) {
				return "";
			}
		}
		if ( _stage.loadStageFromFile( src ) ) {
			_fileName = src;
			_bestTime = getBestTime( src );
			_param.bestTime = _bestTime;
			_lastFile= src;
		}
		return src;
	} //

	protected bool saveReplay() {
		string code = _stage.replayCode;
		if ( code is null ) return false;
		if ( _fileName.length == 0 ) return false;
		string file = _fileName;
		Console.writeln("savefile=%s",file);
		if ( exists( file ) ) {
			string content = cast(string)read( file );
			string chk = split( content , "[body]" )[0];
			if ( chk.length > 1 ) {
				int frm	   = cast(int)atoi( split( chk , "," )[0] );
				Console.writeln("FILE=%s BEST TIME=%d CURRENT TIME=%d" , file , frm , _stage.frameCnt );
				if ( frm <= _stage.frameCnt ) return false;
			}
			remove( file );
		}
		Console.writeln("replaycode:\n" ~ code);
		write( file , code );
		return true;
	} //


	protected IState nextCall( IState caller ) {
		return this;
	} //



	public bool onKeyDown( int keyCode ){
		if ( _stage.mode == GameStage.MODE_REPLAY 
				&& keyCode != KeyCode.SPACE ) {
			switch ( keyCode ) {
				case KeyCode.PAGEDOWN:
					_zoom = -1;
					break;
				case KeyCode.PAGEUP:
					_zoom =  1;
					break;
				case KeyCode.RCTRL:
				case KeyCode.LCTRL:
					_turbo = true;
					break;
				default:
					_returnFlag = true;
					break;
			}
			return true;
		}
		switch ( keyCode ) {
			case KeyCode.SPACE:
				_pauseFlag = true;
				break;
			case KeyCode.ENTER:
			case KeyCode.T_ENTER:
//			case KeyCode.DELETE:
				_returnFlag = true;
				break;
			default:
				return super.onKeyDown( keyCode );
		}
		return true;
	} //
	public bool onKeyUp( int keyCode ) {
		switch( keyCode ) {
			case KeyCode.RCTRL:
			case KeyCode.LCTRL:
				_turbo = false;
				break;
			default:
					return super.onKeyUp( keyCode );
		}
		return true;
	}
	public bool onJoyButtonDown( int id , int button ) {
		int bm = GameConfig.getButtonMap( button );
		if ( _stage.mode == GameStage.MODE_REPLAY && bm != 9 ) {
				_returnFlag = true;
		}
		return super.onJoyButtonDown( id , button );
	} //
	
	public bool onMouseMove( int x , int y , int button) {return true;} //
	public bool onMouseDown( int x , int y , int button) {return true;} //
	public bool onMouseUp( int x , int y , int button) {return true;} //
	public bool onMouseWheel( int x , int y , int delta) {
		if ( delta < 0 ) _stage.zoomIn();
		 else 			 _stage.zoomOut();
		return true;
	} //
} //

public class ReplayGameStage : GameStage {
	protected string _fileName = "";
	protected bool _replayEnd ;
	public bool replayEnd() { return _replayEnd;}
	public string fileName(){ return _fileName; }
	public this( IGraphics g , GameParam p ) {
		super( g , p );
	} //
	protected void initObject( IGraphics g , GameParam p ) {
		super.initObject( g , p);
		_scoreBoard = new ReplayScoreBoard( this , p );
	} //


	public bool loadStage( string src ) {
		bool b = super.loadStage( src );
		string chk = split( src , "[body]" )[0];
		if ( chk.length > 1 ) {
			_param.bestTime = cast(int)atoi( split( chk , "," )[0] );
		}
		
		_vWidthMax  = _cols * Blocks.WIDTH  - Blocks.WIDTH;
		_vHeightMax = _rows * Blocks.HEIGHT - Blocks.HEIGHT;
		return b;
	} //
	
	public bool loadStageFromFile( string fileName ) {
		bool b = super.loadStageFromFile( fileName );
		if ( b ) _fileName = fileName;
		return b;
	} //
	
	public void reloadStage() {
		string f = _fileName;
		_recorder.cut( _frameCnt );
		string d = replayCode;
		reset();
		loadStage( d );
		_fileName = f;
	} //
	
	public void setTas() {
		if ( _mode == MODE_REPLAY ) {
			_mode = MODE_GAME;
			_recorder.cut( _frameCnt );
		}
	} //
	
	public bool action() {
		int m = _mode;
		bool b = super.action();
		if ( m == MODE_REPLAY && _mode != MODE_REPLAY )
					 _replayEnd = true;
		else		 _replayEnd = false;
		return b;
	} //

	public void zoomOut() {
		int vw = _vWidth  + ( _vWidth  >> 5 );
		int vh = _vHeight + ( _vHeight >> 5 );
		if ( vw >= _vWidthMax && vh >= _vHeightMax ) {
			return;
		}
		_vWidth  = vw;
		_vHeight = vh;
		zoomReset();
	} //


} //


public class ReplayPauseScrollState : PauseScrollState {
	public const int CANCEL    = 0;
	public const int PLAY      = 1;
	public const int TAS       = 2;
	public this( GameState state , GameParam param , IGamePad pad , IGraphics graphics ) {
		super( state , param , pad , graphics );
	} //
	public bool onKeyDown( int keyCode ){
		switch ( keyCode ) {
			case KeyCode.A:
				_returnValue = PLAY;
				_returnFlag = true;
				break;
			case KeyCode.T:
				_returnValue = TAS;
				_returnFlag = true;
				break;
			default:
				super.onKeyDown( keyCode );
				break;
		}
		return true;
	} //
	public bool onJoyButtonDown( int id , int button ) {
		int bm = GameConfig.getButtonMap( button );
		if ( bm == 8 ) { 
			_returnValue = TAS;
			_returnFlag = true;
			return true;
		}else
			return super.onJoyButtonDown( id , button );
	} //
	
} //

public class ReplayScoreBoard : ScoreBoard {
	public this( ReplayGameStage stage , GameParam param ) {
		super( stage , param );
	} //

	public bool draw( IGraphics g ) {
		float clipX  = g.viewrect.x  , clipY  = g.viewrect.y , 
			  clipX2 = g.viewrect.x2 , clipY2 = g.viewrect.y2;
		const int W=16;
		const int H=16;

		Player p  = _stage.player;
		_frameCnt = _stage.frameCnt;
		_goldCnt  = _stage.goldCnt;
		_lifeCnt  = _param.life;
		_stageNum = _param.stageNum;
		string filetmp = split( (cast(ReplayGameStage)_stage).fileName , "/")[length-1];
		string file = split ( filetmp , "." )[0];
		_score	  = p.score + _param.score;
		g.setViewport( 0 , 0 , W * 40 , H * 30 ); 
		int bm  = g.blendMode;
		{
			Color c;
			c.color = 0xaa000033;
			g.drawFilledRect( 0 ,  H * 28 , W * 40 , 60 , c );

			g.blendMode = g.ALPHA_ADD;
			c.color = 0xccffff00;
			
			string top	  = format( "FILE=%s  CPU=%s BEST=%s" , file , _stage.cpu , _param.bestTime);
			string bottom  = format( "GOLD %03d   SCORE %06d   TIME %06d" , _goldCnt , _score , _frameCnt ) ;
			string bottom2 = format( "     %03d         %06d        %06d" , _goldCnt , _score , _frameCnt ) ;
			int tx = cast(int)(W * 40 - top.length * W   ) / 2;
			int bx = cast(int)(W * 40 - bottom.length *W ) / 2;

			g.drawText( top    , tx  ,      0 , c , W , H * 2 );
			g.drawText( bottom , bx  , H * 28 , c , W , H * 2 );

			g.blendMode = bm;

			c.color = ( _goldCnt > 0 ) ? 0xccffffff : 0xcc00ffff; 
			g.drawText( bottom2, bx  , H * 28 , c , W , H * 2 );
		}
		g.setViewport( clipX , clipY , clipX2 , clipY2);
		return true;
	} //

} //

